﻿using Architecture.Domain.Dto.Source;
using Architecture.Repository.Impls;
using Architecture.Repository.Interface;
using Architercture.Domain.Service.IService;
using Swork03_28.Domain.TraceableInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architercture.Domain.Service.Impls
{
    public class SupplierService:ISupplierService
    {
        private readonly ISupplierRepository _supplierRepository;

        public SupplierService(ISupplierRepository supplierRepository)
        {
            _supplierRepository = supplierRepository;
        }

        public async Task<List<ImgDto>> GetSupplierImage(string? batchCode,int code)
        {
            var imgStr = await _supplierRepository.GetValues(c => c.BatchCode.Equals(batchCode));
            List<ImgDto> imgList = new List<ImgDto>();
            switch (code)
            {
                //原材料
                case 1:
                    if (imgStr.FirstOrDefault() == null || string.IsNullOrEmpty(imgStr.FirstOrDefault().RawMaterialImage))
                    {
                        return new List<ImgDto>();
                    }
                     imgList = imgStr.FirstOrDefault().RawMaterialImage.Split(",").Select(c => new ImgDto
                    {
                        name = c,
                        url = "https://localhost:44391/Img/" + c
                    }).ToList();
                    break;
                //辅料
                case 2:
                    if (imgStr.FirstOrDefault() == null || string.IsNullOrEmpty(imgStr.FirstOrDefault().AccessoryImage))
                    {
                        return new List<ImgDto>();
                    }
                    imgList = imgStr.FirstOrDefault().AccessoryImage.Split(",").Select(c => new ImgDto
                    {
                        name = c,
                        url = "https://localhost:44391/Img/" + c
                    }).ToList();
                    break;
                //添加剂
                case 3:
                    if (imgStr.FirstOrDefault() == null || string.IsNullOrEmpty(imgStr.FirstOrDefault().AdditiveImage))
                    {
                        return new List<ImgDto>();
                    }
                    imgList = imgStr.FirstOrDefault().AdditiveImage.Split(",").Select(c => new ImgDto
                    {
                        name = c,
                        url = "https://localhost:44391/Img/" + c
                    }).ToList();
                    break;
                //包材
                case 4:
                    if (imgStr.FirstOrDefault() == null || string.IsNullOrEmpty(imgStr.FirstOrDefault().PackingMaterialImage))
                    {
                        return new List<ImgDto>();
                    }
                    imgList = imgStr.FirstOrDefault().PackingMaterialImage.Split(",").Select(c => new ImgDto
                    {
                        name = c,
                        url = "https://localhost:44391/Img/" + c
                    }).ToList();
                    break;
               
            }
            
            return imgList;
        }


        public async Task<int> OperationImg(string? batchCode, string? imgUrls,int code)
        {
            var infor = _supplierRepository.GetValues(c => c.BatchCode.Equals(batchCode)).Result.FirstOrDefault();
            if (infor == null)
            {
                await _supplierRepository.CreateAsync(new SupplierInfo { BatchCode = batchCode });
                infor = _supplierRepository.GetValues(c => c.BatchCode.Equals(batchCode)).Result.FirstOrDefault();
            }
            switch (code)
            {
                case 1:
                    infor.RawMaterialImage = imgUrls;
                    break;
                case 2:
                    infor.AccessoryImage = imgUrls;
                    break;
                case 3:
                    infor.AdditiveImage = imgUrls;
                    break;
                case 4:
                    infor.PackingMaterialImage = imgUrls;
                    break;
            }
            
            int res = await _supplierRepository.UpdateAsync(infor);
            return res;
        }
    }
}
